package edu.mit.nice;

import java.io.IOException;
import java.net.InetAddress;
import java.util.HashMap;

import edu.mit.nice.message.JoinLayerMessage;
import edu.mit.nice.struct.*;

public class NiceClient
{
	private Control control;
	
	private HashMap<Integer, Cluster> layers = new HashMap<Integer, Cluster>();
	private int toplayer;
	
	public NiceClient(int layer)
	{
		this();
		toplayer = layer;
		layers.put(toplayer, new Cluster());
	}
	
	public NiceClient()
	{
		control = new Control(this);
		control.start();
	}

	public void connect(InetAddress host, int port) throws IOException, ClassNotFoundException
	{
		Node hostNode = new Node(host, port);
		
		hostNode.recreate(this); 
		
		joinLayer(hostNode, 0);
	}
	
	private void joinLayer(Node node, int layer) throws IOException, ClassNotFoundException
	{
		node.sendMessage(new JoinLayerMessage(layer));
	}
	
	public void attach(Object leader, int yourLayer)
	{
		// TODO Auto-generated method stub
		
	}

	public Cluster getTopCluster()
	{
		return layers.get(toplayer);
	}

	public Cluster getCluster(int layer) {
		return layers.get(layer);
	}


}